package cn.edu.zut.pms.faker;

import com.github.javafaker.Faker;
import java.sql.*;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Random;

public class FakerLogisticsMonthAmount {
    public static void main(String[] args) {
        // 数据库连接信息（请根据实际环境修改）
        String url = "jdbc:mysql://192.168.17.150:3306/soil?allowPublicKeyRetrieval=true&useSSL=false";
        String user = "root";
        String password = "000000";

        // 创建Faker实例
        Faker faker = new Faker(new Locale("zh_CN"));
        Random random = new Random();
        DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("yyyy-MM");

        // 生成近12个月的月份（例如2024-10至2025-09）
        YearMonth[] months = new YearMonth[12];
        YearMonth current = YearMonth.now();
        for (int i = 0; i < 12; i++) {
            months[i] = current.minusMonths(i);
        }

        try (Connection connection = DriverManager.getConnection(url, user, password)) {
            // 先清空表中现有数据（可选）
            String truncateSql = "TRUNCATE TABLE ads_logistics_month_amount";
            try (Statement statement = connection.createStatement()) {
                statement.execute(truncateSql);
            }

            // 插入数据的SQL语句
            String insertSql = "INSERT INTO ads_logistics_month_amount (" +
                    "month_key, sales_amount" +
                    ") VALUES (?, ?)";

            try (PreparedStatement preparedStatement = connection.prepareStatement(insertSql)) {
                // 基础月销售额范围（受季节影响调整）
                for (YearMonth month : months) {
                    String monthKey = month.format(monthFormatter);
                    int monthValue = month.getMonthValue();
                    double salesAmount;

                    // 模拟季节性销售波动（例如节假日月份销售额更高）
                    if (monthValue == 1 || monthValue == 2 || monthValue == 12) { // 春节、双十二所在月份
                        salesAmount = faker.number().randomDouble(2, 800000, 1500000);
                    } else if (monthValue == 6 || monthValue == 11) { // 618、双十一所在月份
                        salesAmount = faker.number().randomDouble(2, 600000, 1200000);
                    } else { // 普通月份
                        salesAmount = faker.number().randomDouble(2, 400000, 800000);
                    }

                    // 设置参数
                    preparedStatement.setString(1, monthKey);
                    preparedStatement.setDouble(2, salesAmount);

                    preparedStatement.executeUpdate();
                }
                System.out.println("数据插入完成，共插入 " + months.length + " 条月度销售金额记录");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}